{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import symbols, sin, cos, Abs\n",
    "from galgebra.ga import Ga\n",
    "from galgebra.printer import Format, Fmt\n",
    "from IPython.display import Latex\n",
    "Format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0\\\\0 & 1 & 0\\\\0 & 0 & 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1, 0, 0],\n",
       "[0, 1, 0],\n",
       "[0, 0, 1]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xyz_coords = (x, y, z) = symbols('x y z', real=True)\n",
    "(o3d, ex, ey, ez) = Ga.build('e', g=[1, 1, 1], coords=xyz_coords, norm=True)\n",
    "o3d.g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} f = f  \\end{equation*}"
      ],
      "text/plain": [
       "f"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = o3d.mv('f', 'scalar', f=True)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2} = \\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla^{2} = D{x}^2 + D{y}^2 + D{z}^2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = o3d.mv('F', 'vector', f=True)\n",
    "lap = o3d.grad*o3d.grad\n",
    "lap.Fmt(1,r'\\nabla^{2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2} = \\frac{\\partial^{2}}{\\partial x^{2}} + \\frac{\\partial^{2}}{\\partial y^{2}} + \\frac{\\partial^{2}}{\\partial z^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla^{2} = D{x}^2 + D{y}^2 + D{z}^2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lap.Fmt(1,r'\\nabla^{2}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\partial^{2}_{x} f  + \\partial^{2}_{y} f  + \\partial^{2}_{z} f  \\end{equation*}"
      ],
      "text/plain": [
       "D{x}^2f + D{y}^2f + D{z}^2f"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = lap*f\n",
    "lapf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla \\cdot (\\nabla f) = \\partial^{2}_{x} f  + \\partial^{2}_{y} f  + \\partial^{2}_{z} f  \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla \\cdot (\\nabla f) = D{x}^2f + D{y}^2f + D{z}^2f"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = o3d.grad | (o3d.grad * f)\n",
    "lapf.Fmt(1,r'\\nabla \\cdot (\\nabla f)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} x = = \\partial_{x} F^{x}  + \\partial_{y} F^{y}  + \\partial_{z} F^{z}  \\end{equation*}"
      ],
      "text/plain": [
       "x = = D{x}F__x + D{y}F__y + D{z}F__z"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "divF = o3d.grad|F\n",
    "divF.Fmt(1,'x =')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla F = \\left ( \\partial_{x} F^{x}  + \\partial_{y} F^{y}  + \\partial_{z} F^{z} \\right )  + \\left ( - \\partial_{y} F^{x}  + \\partial_{x} F^{y} \\right ) \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{y} + \\left ( - \\partial_{z} F^{x}  + \\partial_{x} F^{z} \\right ) \\boldsymbol{e}_{x}\\wedge \\boldsymbol{e}_{z} + \\left ( - \\partial_{z} F^{y}  + \\partial_{y} F^{z} \\right ) \\boldsymbol{e}_{y}\\wedge \\boldsymbol{e}_{z} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla F = D{x}F__x + D{y}F__y + D{z}F__z + (-D{y}F__x + D{x}F__y)*e_x^e_y + (-D{z}F__x + D{x}F__z)*e_x^e_z + (-D{z}F__y + D{y}F__z)*e_y^e_z"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gradF = o3d.grad * F\n",
    "gradF.Fmt(1,r'\\nabla F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 & 0 & 0\\\\0 & r^{2} & 0\\\\0 & 0 & r^{2} \\sin^{2}{\\left(\\theta \\right)}\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([\n",
       "[1,    0,                  0],\n",
       "[0, r**2,                  0],\n",
       "[0,    0, r**2*sin(theta)**2]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sph_coords = (r, th, phi) = symbols('r theta phi', real=True)\n",
    "(sp3d, er, eth, ephi) = Ga.build('e', g=[1, r**2, r**2 * sin(th)**2], coords=sph_coords, norm=True)\n",
    "sp3d.g_raw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla = \\boldsymbol{e}_{r} \\frac{\\partial}{\\partial r} + \\boldsymbol{e}_{\\theta } \\frac{1}{r} \\frac{\\partial}{\\partial \\theta } + \\boldsymbol{e}_{\\phi } \\frac{1}{r \\sin{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\phi } \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla = e_r*D{r} + e_theta*1/r*D{theta} + e_phi*1/(r*sin(theta))*D{phi}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp3d.grad.Fmt(1,r'\\nabla')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2}  = \\frac{2}{r} \\frac{\\partial}{\\partial r} + \\frac{1}{r^{2} \\tan{\\left (\\theta  \\right )}} \\frac{\\partial}{\\partial \\theta } + \\frac{1}{r^{2} {\\sin{\\left (\\theta  \\right )}}^{2}} \\frac{\\partial^{2}}{\\partial \\phi ^{2}} + \\frac{\\partial^{2}}{\\partial r^{2}} + r^{-2} \\frac{\\partial^{2}}{\\partial \\theta ^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla^{2}  = 2/r*D{r} + 1/(r**2*tan(theta))*D{theta} + 1/(r**2*sin(theta)**2)*D{phi}^2 + D{r}^2 + r**(-2)*D{theta}^2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = sp3d.mv('f', 'scalar', f=True)\n",
    "F = sp3d.mv('F', 'vector', f=True)\n",
    "B = sp3d.mv('B', 'bivector', f=True)\n",
    "sp3d.grad.Fmt(1,r'\\nabla')\n",
    "lap = sp3d.grad*sp3d.grad\n",
    "lap.Fmt(1,r'\\nabla^{2} ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla^{2} f = \\frac{r^{2} \\partial^{2}_{r} f  + 2 r \\partial_{r} f  + \\partial^{2}_{\\theta } f  + \\frac{\\partial_{\\theta } f }{\\tan{\\left (\\theta  \\right )}} + \\frac{\\partial^{2}_{\\phi } f }{{\\sin{\\left (\\theta  \\right )}}^{2}}}{r^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla^{2} f = (r**2*D{r}^2f + 2*r*D{r}f + D{theta}^2f + D{theta}f/tan(theta) + D{phi}^2f/sin(theta)**2)/r**2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Lapf = lap*f\n",
    "Lapf.Fmt(1,r'\\nabla^{2} f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla \\cdot (\\nabla f) = \\frac{r^{2} \\partial^{2}_{r} f  + 2 r \\partial_{r} f  + \\partial^{2}_{\\theta } f  + \\frac{\\partial_{\\theta } f }{\\tan{\\left (\\theta  \\right )}} + \\frac{\\partial^{2}_{\\phi } f }{{\\sin{\\left (\\theta  \\right )}}^{2}}}{r^{2}} \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla \\cdot (\\nabla f) = (r**2*D{r}^2f + 2*r*D{r}f + D{theta}^2f + D{theta}f/tan(theta) + D{phi}^2f/sin(theta)**2)/r**2"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lapf = sp3d.grad | (sp3d.grad * f)\n",
    "lapf.Fmt(1,r'\\nabla \\cdot (\\nabla f)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla F = \\partial_{x} F^{x}  + \\partial_{y} F^{y}  + \\partial_{z} F^{z}  \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla F = D{x}F__x + D{y}F__y + D{z}F__z"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dviF = sp3d.grad | F\n",
    "divF.Fmt(1,r'\\nabla F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla \\wedge F = \\frac{r \\partial_{r} F^{\\theta }  + F^{\\theta }  - \\partial_{\\theta } F^{r} }{r} \\boldsymbol{e}_{r}\\wedge \\boldsymbol{e}_{\\theta } + \\frac{r \\partial_{r} F^{\\phi }  + F^{\\phi }  - \\frac{\\partial_{\\phi } F^{r} }{\\sin{\\left (\\theta  \\right )}}}{r} \\boldsymbol{e}_{r}\\wedge \\boldsymbol{e}_{\\phi } + \\frac{\\frac{F^{\\phi } }{\\tan{\\left (\\theta  \\right )}} + \\partial_{\\theta } F^{\\phi }  - \\frac{\\partial_{\\phi } F^{\\theta } }{\\sin{\\left (\\theta  \\right )}}}{r} \\boldsymbol{e}_{\\theta }\\wedge \\boldsymbol{e}_{\\phi } \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla \\wedge F = (r*D{r}F__theta + F__theta - D{theta}F__r)*e_r^e_theta/r + (r*D{r}F__phi + F__phi - D{phi}F__r/sin(theta))*e_r^e_phi/r + (F__phi/tan(theta) + D{theta}F__phi - D{phi}F__theta/sin(theta))*e_theta^e_phi/r"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "curlF = sp3d.grad ^ F\n",
    "curlF.Fmt(1,r'\\nabla \\wedge F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\nabla \\cdot B = - \\frac{\\frac{B^{r\\theta } }{\\tan{\\left (\\theta  \\right )}} + \\partial_{\\theta } B^{r\\theta }  + \\frac{\\partial_{\\phi } B^{r\\phi } }{\\sin{\\left (\\theta  \\right )}}}{r} \\boldsymbol{e}_{r} + \\frac{r \\partial_{r} B^{r\\theta }  + B^{r\\theta }  - \\frac{\\partial_{\\phi } B^{\\theta \\phi } }{\\sin{\\left (\\theta  \\right )}}}{r} \\boldsymbol{e}_{\\theta } + \\frac{r \\partial_{r} B^{r\\phi }  + B^{r\\phi }  + \\partial_{\\theta } B^{\\theta \\phi } }{r} \\boldsymbol{e}_{\\phi } \\end{equation*}"
      ],
      "text/plain": [
       "\\nabla \\cdot B = -(B__rtheta/tan(theta) + D{theta}B__rtheta + D{phi}B__rphi/sin(theta))*e_r/r + (r*D{r}B__rtheta + B__rtheta - D{phi}B__thetaphi/sin(theta))*e_theta/r + (r*D{r}B__rphi + B__rphi + D{theta}B__thetaphi)*e_phi/r"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "divB = sp3d.grad | B\n",
    "divB.Fmt(1,r'\\nabla \\cdot B')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} F = F^{r}  \\boldsymbol{e}_{r} + F^{\\theta }  \\boldsymbol{e}_{\\theta } + F^{\\phi }  \\boldsymbol{e}_{\\phi } \\end{equation*}"
      ],
      "text/plain": [
       "F__r*e_r + F__theta*e_theta + F__phi*e_phi"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} F =  \\begin{aligned}[t]  & F^{r}  \\boldsymbol{e}_{r} \\\\  &  + F^{\\theta }  \\boldsymbol{e}_{\\theta } \\\\  &  + F^{\\phi }  \\boldsymbol{e}_{\\phi }  \\end{aligned}  \\end{equation*}"
      ],
      "text/plain": [
       "F =  F__r*e_r\n",
       " + F__theta*e_theta\n",
       " + F__phi*e_phi"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.Fmt(3,'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sqrt{\\left(\\operatorname{F^{\\phi}}\\right)^{2}{\\left(r,\\theta,\\phi \\right)} + \\left(\\operatorname{F^{r}}\\right)^{2}{\\left(r,\\theta,\\phi \\right)} + \\left(\\operatorname{F^{\\theta}}\\right)^{2}{\\left(r,\\theta,\\phi \\right)}}$"
      ],
      "text/plain": [
       "   __________________________________________________\n",
       "  ╱     2                2                2          \n",
       "╲╱  F__φ (r, θ, φ) + F__r (r, θ, φ) + F__θ (r, θ, φ) "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F.norm()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
